package com.qq.BFMRSE.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.qq.BFMRSE.model.BloomFilter;
import com.qq.BFMRSE.model.encryptedIndex;
import com.qq.BFMRSE.model.trapDoor;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class paraseJsonUtil {	
	public static Map<String,encryptedIndex> Json2MapIndex(String jsonString)
{
	Map<String,encryptedIndex> resultMap=new HashMap<String, encryptedIndex>();
	JSONObject jObject=JSONObject.fromObject(jsonString);
	Iterator<String> iterator=jObject.keys();
	while(iterator.hasNext())
	{
		String tempKey=iterator.next();
		JSONObject tempJo=JSONObject.fromObject(jObject.get(tempKey));
		encryptedIndex tempenIndex=(encryptedIndex) tempJo.toBean(tempJo, encryptedIndex.class);
		resultMap.put(tempKey, tempenIndex);
	}
	return resultMap;
}
public static Map<String,List<Integer>> Json2MapBitSet(String jsonString)
{
	Map<String,List<Integer>> resultMap=new HashMap<String,List<Integer>>();
	JSONObject jObject=JSONObject.fromObject(jsonString);
	Iterator<String> iterator=jObject.keys();
	while(iterator.hasNext())
	{
		String tempKey=iterator.next();
		List<Integer> templist=paraseBitSetString(jObject.get(tempKey).toString());
		resultMap.put(tempKey,templist);
	}
	return resultMap;
}
public static List<Integer>paraseBitSetString(String bitString)
{
	List<Integer>resultList=new ArrayList<Integer>();
	String[]temp=bitString.substring(1, bitString.length()-1).split(",");
	for(int i=0;i<temp.length;i++)
	{
		Integer integer=new Integer(temp[i].trim());
		resultList.add(integer);
	}
	return resultList;
	
}
public static List<Integer>parasePathStack(String path)
{
	
	JSONArray ja=JSONArray.fromObject(path);
	List<Integer>list=ja.toList(ja);
	return list;
}
public static trapDoor paraseJson2Trap(String searchTrap,String trapBitInfo)
{
	JSONObject jo1=JSONObject.fromObject(searchTrap);
    trapDoor reDoor=(trapDoor)jo1.toBean(jo1, trapDoor.class);
    JSONArray jaArray=JSONArray.fromObject(trapBitInfo);
    List<Integer> list=jaArray.toList(jaArray);
    BloomFilter bFilter=new BloomFilter();
    Iterator<Integer>iterator=list.iterator();
    while(iterator.hasNext())
    {
    	bFilter.getBits().set(iterator.next());
    }
    reDoor.setBf(bFilter);
    return reDoor;
}
}